'use strict';

var initDb = require('./utils/initDb');

initDb();
var Crawler = require('crawler');
var article = require('./models/articles');


for(let index = 1 ; index<=4 ; index++){
    var c = new Crawler({
        reteLimit:2000,
        maxConnections : 10,
        callback : function(error,res,done){
            if(error){
                console.log(error);
            }else{
                var $ = res.$;
                var list = $(' .dw_table .el ').toArray();
    
                list.forEach(element => {
                    var txt = $(element).find(' .t1 a ').attr('title');//职业
                    var txt1 = $(element).find(' .t2 a ').text().trim().replace(/([/][^/]+)/g,'');//公司
                    var txt2 = $(element).find(' .t3 ').text().trim().replace(/([/][^/]+)/g,'');//地址
                    var txt3 = $(element).find(' .t4 ').text().trim().replace(/([/][^/]+)/g,'');//薪资
                    var txt4 = $(element).find(' .t5 ').text().trim().replace(/([/][^/]+)/g,'');//发布时间
                    article.create({title:txt,company:txt1,Address:txt2,Salary:txt3,Releasetime:txt4});
                    console.log(txt,txt1,txt2,txt3,txt4);
                });
    
                console.log(list.length);
    
                //console.log($("title").text());
            }
            done();
        }
    });
    c.queue('https://search.51job.com/list/030000,000000,0000,00,9,99,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590%25E5%25B8%2588,2,'+index+'.html?lang=c&stype=&postchannel=0100&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=');
}
    
